﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wicture.QPZS.EPC.API.Domain.Entities;
using Wicture.QPZS.EPC.API.Repository;

namespace Wicture.QPZS.EPC.API.Throtteds
{
    public class TimesCostThrottled : CostThrottled
    {
        private readonly IServiceRepository _serviceRepository;
        private const int DefaultCostTimes = 1;

        public TimesCostThrottled(IServiceRepository serviceRepository)
        {
            _serviceRepository = serviceRepository;
        }

        public override List<ServicePackageType> ServicePackageTypes => new List<ServicePackageType> {
            ServicePackageType.包时,
            ServicePackageType.包次
        };

        public override void InvokeMethod(ThrottlingContext context)
        {
            CostApiTimes(context);
            CostPackageTimes(context);
        }

        private void CostApiTimes(ThrottlingContext context)
        {
            _serviceRepository.AddApiConsumed(context.AccountPackageApi.Id, DefaultCostTimes);
        }

        private void CostPackageTimes(ThrottlingContext context)
        {
            _serviceRepository.AddPackageConsumed(context.AccountPackage.Id, DefaultCostTimes);
        }
    }
}
